(function(context) {
	var pluginsPath, error = false, executeing = false, htmls = [];
	if (typeof (code) !== 'undefined' && code !== '' && code !== null)
		error = true;
	if (!error) {
		pluginsPath = basepath + '/plugins/pcb'
		if (show === 'off') {// 显示"电话咨询"按钮
			htmls
					.push('<div class="free_ask" style="cursor:pointer; width:100px; text-align:center;  color:#fff; line-height:30px; height:30px; background:'
							+ buttonColor + ';">电话咨询</div>');
		}
		htmls
				.push('<div class="free_search"><div class="htDiv"><span class="span_tel"><img src="'
						+ pluginsPath
						+ '/img/tel.png" width="37" height="37" id="pic"></span><span class="span_theme">'
						+ title + '</span>');
		if (show === 'off') {// 显示“关闭图标”
			htmls
					.push('<span style="float:left;display:block;height:53px;width:30px;line-height:53px;text-align:center;vertical-align:middle;"><img src="'
							+ pluginsPath
							+ '/img/zx_close.png" width="13px" height="16px" style="vertical-align:middle;cursor:pointer" id="close" /></span>');
			// htmls.push('<a href="javascript:void(0)" class="close_btn"
			// id="close"></a>');
		}
		htmls.push('</div><div class="zx_nr"><div class="f_step1"><p>');
		htmls.push(content);
		htmls
				.push('</p><form action="" id="returnCallFrom" name="returnCallFrom">');
		htmls.push('<div class="inform_div"><dl>');
		htmls
				.push('<dt><input name="mobile" id="mobile" type="text" class="i_txt" placeholder="请输入您的号码"  style="width:213px;"/></dt>');
		htmls
				.push('</dl><dl><dt><input name="randCode" id="randCodeConsult" type="text" class="i_txt1" placeholder="请输入验证码" />');
		htmls
				.push('<img id="randCodeImgConsult" onclick="refreshRandCodeConsult()" class="check_code" src="'
						+ basepath
						+ '/createCaptchaCode?uuid='
						+ uuid
						+ '" /><span class="codeError" style="display:none" id="codeError"></span>');
		htmls
				.push('</dt></dl><a href="javascript:void(0)" class="btn_zixun" id="RealMianFeiZiXun">免费咨询</a></div></form></div><div class="f_step2" style="display:none">');
		htmls
				.push('<p>正在呼叫，请注意接听来电</p><img src="'
						+ pluginsPath
						+ '/img/tel_ing1.jpg" width="182" height="47"><a class="btn_cancel mt54">关闭</a></div></div></div>');
	}

	function validateNumber(number) {
		var checkResult = {
			success : true,
			msg : '号码校验通过'
		};
		if (number === null || number === '') {
			checkResult.success = false;
			checkResult.msg = '请输入电话号码';
			return checkResult;
		}
		if (/[^0-9]/.test(number)) {// 存在非数字
			checkResult.success = false;
			checkResult.msg = '您输入的号码有误，请重新输入';
			return checkResult;
		}
		var l = number.length;
		if ((l === 7 || l === 8) && number.charAt(0) !== '0') {
			checkResult.success = false;
			checkResult.msg = '您输入的号码有误，若输入固话号码，请加区号';
		} else if (l !== 11 && l !== 12) {
			checkResult.success = false;
			checkResult.msg = '您输入的号码有误，请重新输入';
		} else if (l === 12 && number.charAt(0) !== '0') {
			checkResult.success = false;
			checkResult.msg = '您输入的号码有误，请重新输入';
		}
		return checkResult;
	}

	function loadScript(src) {
		var oHead = document.getElementsByTagName('head')[0];
		var oScript = document.createElement("script");
		oScript.type = "text/javascript";
		oScript.src = src;
		oHead.appendChild(oScript);
	}

	context.$dudu = {
		create : function(id) {
			if (error) {
				alert('嘟嘟平台未授权当前用户使用网页回呼插件，因为[' + describe + ']');
				return;
			}
			var context = $('#' + id) || $('.' + id);
			if (context) {
				context.html(htmls.join(''));
				setTimeout(
						function() {// 所有元素加载完毕
							$('.btn_zixun')
									.css('background-color', buttonColor);
							$('.btn_cancel').css('background-color',
									buttonColor);
							loadScript(pluginsPath + '/js/ieplus.js');
							var props = {};
							if (bottom0 !== '-1')
								props.bottom = bottom0 + 'px';
							if (top0 !== '-1')
								props.top = top0 + 'px';
							$('.free_search').css(props);
							var error = $('.codeError'), p1 = $('.f_step1'), p2 = $('.f_step2'), sessionId = null, start0 = {
								left : '-352px'
							}, move0 = {
								left : offset + 'px'
							}, titleContext = $('.span_theme'), mobileContext = $('#mobile');
							if (direction0 === 'left') {
								start0 = {
									right : '-352px'
								};
								move0 = {
									right : offset + 'px'
								};
							}
							if (show === 'off') {
								// $('#close').css({
								// 'background':'url('+pluginsPath+'/img/zx_close.png)
								// 0 center no-repeat',
								// 'background-size':'14px 14px'
								// });
								$('.free_ask').click(
										function() {
											$('.free_search').animate(move0,
													200).delay(200).animate({
												height : '335px'
											}, 200);
										});
								$('.span_tel').css('cursor', 'pointer');
								$('.span_tel,.btn_cancel,#close')
										.click(
												function() {
													$('.free_search').animate({
														height : '335px'
													}, 200).delay(100).animate(
															start0, 200);
													p2.hide();
													p1.show();
													$("#randCodeConsult").val(
															'');
													if (sessionId) {
														$
																.ajax({
																	type : "get",
																	scriptCharset : "UTF-8",
																	url : basepath
																			+ "/DIAL/dialStop.do",
																	data : {
																		sessionid : sessionId,
																	},
																	dataType : "jsonp",
																	complete : function() {
																		sessionId = null;
																		titleContext
																				.text(title);
																	}
																});
													}
												});
							} else {
								$('.free_search').animate(move0, 200)
										.delay(200).animate({
											height : '335px'
										}, 200);
								$('.btn_cancel').click(function() {
									p2.hide();
									p1.show();
									$("#randCodeConsult").val('');
									$.ajax({
										type : "get",
										scriptCharset : "UTF-8",
										url : basepath + "/DIAL/dialStop.do",
										data : {
											sessionid : sessionId,
										},
										dataType : "jsonp",
										complete : function() {
											sessionId = null;
											titleContext.text(title);
										}
									});
								});
							}
							$(document.body)
									.on(
											'input',
											'#mobile,#randCodeConsult',
											function() {
												var obj = $(this), value = obj
														.val();
												if (value === '') {
													obj.css('color',
															'rgb(200,200,200)');
												} else {
													obj.css('color',
															'rgb(51,51,51)');
												}
											});
							$('.btn_zixun')
									.click(
											function() {
												if (executeing)// 正在执行中
													return;
												executeing = true;
												var currentTarget = $(this);
												currentTarget.css(
														'background-color',
														'rgb(197, 196, 196)');
												var mobile = $('#mobile').val(), result = validateNumber(mobile);
												if (!result.success) {
													error.show();
													error.text(result.msg);
													executeing = false;
													currentTarget
													.css(
															'background-color',
															buttonColor);
													return;
												}
												var randCode = $(
														"#randCodeConsult")
														.val();
												if (randCode === '') {
													error.show();
													error.text('请输入验证码');
													executeing = false;
													currentTarget
													.css(
															'background-color',
															buttonColor);
													return;
												}
												var caller0 = phones;
												var called0 = mobile;
												if (callOrder === 'called') {
													caller0 = mobile;
													called0 = phones;
												}
												$
														.ajax({
															type : "get",
															url : pluginsPath
																	+ "/createTwonessCommunicate.do",
															data : {
																uuid : uuid,
																randCode : randCode,
																cust_account : cust_account,
																timestamp : timestamp,
																sign : sign,
																caller : caller0,
																called : called0,
																app_key : app_key,
																ext_terminalCode : caller0,
																display_mode : display_mode
															// 显示配置号码
															},
															dataType : "jsonp",
															scriptCharset : "UTF-8",
															contentType : "application/x-www-form-urlencoded; charset=UTF-8",
															success : function(
																	data) {
																if (data.result != '0') {
																	error
																			.show();
																	error
																			.text(data.describe);
																} else {
																	error
																			.hide();
																	mobileContext
																			.val('');
																	p1.hide();
																	p2.show();
																	sessionId = data.sessionId;
																	if (data.displayNbr
																			&& data.displayNbr !== '') {
																		titleContext
																				.text(data.displayNbr);
																	}
																}
															},
															complete : function() {
																currentTarget
																		.css(
																				'background-color',
																				buttonColor);
																executeing = false;
																refreshRandCodeConsult();
															}

														});

											});

						}, 0);
			}
		}
	}

	context.refreshRandCodeConsult = function() {
		// 生成验证码
		$('#randCodeImgConsult').hide().attr(
				'src',
				basepath + '/createCaptchaCode?date=' + new Date() + "&uuid="
						+ uuid).fadeIn();
	};

})(window);
